package com.lagouedu.demoredis.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.cache.Cache;
import com.lagouedu.demoredis.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;

@Component
@Order(100)
public class InitCache implements ApplicationRunner {
    Logger logger = LoggerFactory.getLogger(InitCache.class);

    @Autowired
    private Cache cache;

    @Autowired
    private JedisCluster jedisCluster;

    @Async
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {

        String s = JsonUtil.readJsonFromClassPath("navigation.json", String.class);
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONArray menus = jsonObject.getJSONArray("menus");
        logger.info("================================初始化redis===============================");
        jedisCluster.set("menus",menus.toJSONString());
        logger.info("================================初始化Guava===============================");
        cache.put("menus",menus.toJSONString());
        logger.info("菜单数据初始化成功：{}",menus);
    }
}
